/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;

import com.sgsmcan.Entidad.Amo;
import com.sgsmcan.Hibernate.HibernateUtil;
import com.sgsmcan.Utils.Utilitario;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author user
 */
public class AmoDaoImpl extends HibernateDao<Amo> implements AmoDao<Amo>{

    @Override
    public Amo getAmo(Integer id) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Amo.class,"amo").add(Restrictions.eq("amo.id", id));
            return (Amo)criteria.uniqueResult();
        }catch(HibernateException e){
            Utilitario.LOG.error("Error en PerroDaoImpl", e);
            throw  new HibernateException(e);
        }finally{
            
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public DetachedCriteria getAmos() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Amo.class,"amo").add(Restrictions.eq("amo.estado",Boolean.TRUE));
        return criteria;
    }

    @Override
    public boolean existeDni(String dni) {
        
        boolean existe=false;
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Amo.class,"amo").add(Restrictions.eq("amo.dni", dni)).add(Restrictions.eq("amo.estado",Boolean.TRUE));
            Amo amo = (Amo) criteria.uniqueResult();
            if(amo != null){
                existe = true;
            }
        }catch(HibernateException e){
            Utilitario.LOG.error("Error en PerroDaoImpl", e);
            throw  new HibernateException(e);
        }finally{
            
            if(session != null){
                session.close();
            }
        }
        return existe;
    }
    
}
